<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
</head>

<body>
    <div class="nav">
        <img src="image/image1.jpg" alt="">
        <span class="title">个人博客系统</span>
        <span class="spacer"></span>
    </div>

    <div class="login-container">
        <div class="login-dialog">
                <!-- 标题 -->
                <h3>注册</h3>
                <!-- 输入用户名 -->
                <div class="row">
                    <span>用户名</span>
                    <input type="text" id="username" name="username">
                </div>
                <!-- 输入密码 -->
                <div class="row">
                    <span>密码</span>
                    <input type="password" id="password" name="password">
                </div>
                <div class="row">
                    <span>确认密码</span>
                    <input type="password" id="password2" name="password2">
                </div>
                <!-- 提交按钮 -->
                <div class="row submit-row">
                    <button id="submit" onclick="register()">注册</button>
                </div>
        </div>
    </div>
</body>
<script src="js/jquery.min.js"></script>
<script>
    function register() {
        var username = jQuery("#username");
        var password = jQuery("#password");
        var password2 = jQuery("#password2");
        if (jQuery.trim(username.val()) == "") {
            alert("抱歉：请先输入登录名！");
            username.focus();
            return;
        }
        if (jQuery.trim(password.val()) == "") {
            alert("抱歉：请先输入密码！");
            password.focus();
            return;
        }
        if (jQuery.trim(password2.val()) == "") {
            alert("抱歉：请先输入确认密码！");
            password2.focus();
            return;
        }
        if (password.val().length < 8) {
            alert("抱歉：密码强度太低，密码位数必须大于等于8位！");
            password.focus();
            return;
        }
        if (password.val() != password2.val()) {
            alert("抱歉：两次输入的密码不一致，请检查！");
            password.focus();
            return;
        }
        jQuery.ajax({
            url: "user/register",
            type: "POST",
            data: {
                "username": username.val(),
                "password": password.val()
            },
            success: function (result) {
                if (result != null && result.success == 200){
                    if(result.data = 1){
                        alert("注册成功！");
                        location.href = "login.html";
                    }else{
                        alert("抱歉：注册失败，"+data.message);
                    }
                }else{
                    alert("后台程序出错！");  
                }
            }
        })
    }
</script>

</html>